package server;
import java.io.*;
import java.util.Arrays;

import msgs.*;
/**
 * BANS USERS!
 * Team 9: Andrew Hayworth, Brian Parrella, Ryan Kortmann, Nina Papa
 * @author Andrew Hayworth
 */
public class BanHammer extends Thread implements TTConstants {
	private BanMsg bmsg;
	
	public BanHammer(AdminMsg amsg) {
		this.bmsg = (BanMsg) amsg;
	}
	
	public void run() {
		// first see if they're who they say they are 
		if (!(Arrays.equals(bmsg.getAdminKey(), TTServer.adminTable.get(bmsg.getName()).getClientKey()))) {
			bmsg.setResponse(Responses.DENY);
			AdminSender.outQ.add((AdminMsg) bmsg);
			return;
		}
		
		//cool, ban the user.
		switch(bmsg.getAction()) {
		case BANHAMMER:
			if (!(TTServer.banHammer.contains(bmsg.getUname()))) {
				synchronized(TTConstants.lock) {
					//add them to the table and write it to disk
					TTServer.banHammer.add(bmsg.getUname());
					TTServer.writeObject(new File("data/banHammer.dat"), TTServer.banHammer);
				}
			}
			break;
		case UNBANHAMMER:
			// show them mercy
			if (TTServer.banHammer.contains(bmsg.getUname())) {
				synchronized(TTConstants.lock) {
					// unban them, then write to disk
					TTServer.banHammer.remove(bmsg.getUname());
					TTServer.writeObject(new File("data/banHammer.dat"), TTServer.banHammer);
				}
			}
			break;
		}
		
		bmsg.setResponse(Responses.OK);
		AdminSender.outQ.add((AdminMsg) bmsg);
	}
}
